<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表页</title>
    <link rel="stylesheet" href="css/blog_common.css">
    <link rel="stylesheet" href="css/blog_list.css">
</head>
<body>
    <!-- 导航栏部分 -->
    <div class="nav">
        <img src="image/img_1.jpg" alt="">
        <span class="title">我的博客系统</span>
        <!-- 使用这个标签仅仅用于占位，把后面的几个a标签挤到右侧 -->
        <div class="space"></div>
        <a href="blog_list.html">主页</a>
        <a href="blog_edit.html">写博客</a>
        <a href="logout">注销</a>
    </div>

    <!-- 页面主体部分 -->
    <div class="container">
        <!-- 左侧板块信息 -->
        <div class="container-left">
            <!-- 使用card 表示用户信息 -->
            <div class="card">
                <!-- 用户头像 -->
                <img src="image/img_1.jpg" alt="">
                <!-- 用户名 -->
                <h3></h3>
                <a href="#">github 地址</a>
                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="counter">
                    <span class="blog_count"></span>
                    <span>1</span>
                </div>
            </div>
        </div>
        <!-- 右侧板块信息 -->
        <div class="container-right">
        </div>
    </div>
    <script src="./js/jqurery.min.js"></script>
    <script>
        function getBlogList() {
            $.ajax({
                method: 'get',
                url:'blog',
                success: function(body){
                    // 此处的响应正文body原本是一个json格式的字符串，但此时被Jquery自动解析成一个json对象数组
                    let containerRight = document.querySelector('.container-right');
                    for(let blog of body){
                        let blogDiv = document.createElement('div');
                        blogDiv.className = 'blog';
                        let tittleDiv = document.createElement('div');
                        tittleDiv.className = 'tittle';
                        tittleDiv.innerHTML = blog.tittle;
                        blogDiv.appendChild(tittleDiv);

                        let dateDiv = document.createElement('div');
                        dateDiv.className = 'date';
                        dateDiv.innerHTML = blog.postTime;
                        blogDiv.appendChild(dateDiv);

                        let descDiv = document.createElement('div');
                        descDiv.className = 'desc';
                        descDiv.innerHTML = blog.content;
                        blogDiv.appendChild(descDiv);

                        let a = document.createElement('a');
                        a.innerHTML = '查看全文 &gt;&gt;';
                        a.href = 'blog_content.html?blogId=' + blog.blogId;
                        blogDiv.appendChild(a);

                        containerRight.appendChild(blogDiv);
                    }
                }
            });
        }

        getBlogList();

        function checkLogin(){
            $.ajax({
                method: "get",
                url: "login",
                success: function(body){
                    if(body.userId && body.userId > 0){
                        console.log("当前用户已经登录！");
                        let h3 = document.querySelector('.container-left .card h3');
                        h3.innerHTML = body.username;
                    }else{
                        location.assign("blog_login.html");
                    }
                }
            });
        }

        checkLogin();

        function getCount(){
            $.ajax({
                method: "get",
                url: "counter",
                success: function(body){
                    console.log("counter111:" + body);
                    console.log("counter111:" + body.counter);
                    let bodyTemp = JSON.parse(body);
                    console.log("bodyTemp111:" + bodyTemp);
                    console.log("bodyTemp111:" + bodyTemp.counter);
                    let blog_count = document.querySelector('.container-left .card .counter .blog_count');
                    blog_count.innerHTML = bodyTemp.counter;
                }
            });
        }

        getCount();
    </script>
</body>
</html>